/*******************************************************************
 *   > File Name: key.c
 *   > Author: fly
 *   > Mail: XXXXXXXX@icode.com
 *   > Create Time: 2020年07月03日 星期五 12时45分16秒
 ******************************************************************/

#include <key.h>

void key_init(void)
{
	/*
	 * LEFT ->EINT2   -> GPH0_2
	 * DOWN ->EINT3   -> GPH0_3
	 * UP   ->KP_COL0 -> GPH2_0
	 * RIGHT->KP_COL1 -> GPH2_1
	 * BACK ->KP_COL2 -> GPH2_2
	 * MENU ->KP_COL3 -> GPH2_3
	 * */
	/* LEFT */
	writel(S5PV210_GPH2CON, (readl(S5PV210_GPH0CON) & ~(0xf<<8)) | (0x0<<8));
	writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH0PUD) & ~(0x3<<4)) | (0x2<<4));
	/* DOWN */
	writel(S5PV210_GPH2CON, (readl(S5PV210_GPH0CON) & ~(0xf<<12)) | (0x0<<12));
	writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH0PUD) & ~(0x3<<6)) | (0x2<<6));
	/* UP */
	writel(S5PV210_GPH2CON, (readl(S5PV210_GPH2CON) & ~(0xf<<0)) | (0x0<<0));
	writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH2PUD) & ~(0x3<<0)) | (0x2<<0));
	/* RIGHT */
	writel(S5PV210_GPH2CON, (readl(S5PV210_GPH2CON) & ~(0xf<<4))| (0x0<<4));
	writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH2PUD) & ~(0x3<<2)) | (0x2<<2));
	/* BACK */
	writel(S5PV210_GPH2CON, (readl(S5PV210_GPH2CON) & ~(0xf<<8))| (0x0<<8));
	writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH2PUD) & ~(0x3<<4)) | (0x2<<4));
	/* MENU */
	writel(S5PV210_GPH2CON, (readl(S5PV210_GPH2CON) & ~(0xf<<12))| (0x0<<12));
	writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH2PUD) & ~(0x3<<6)) | (0x2<<6));
}

int key_left_pressed(void)
{
	return !(readl(S5PV210_GPH0DAT) & (0x1<<2));
}

int key_down_pressed(void)
{
	return !(readl(S5PV210_GPH0DAT) & (0x1<<3));
}

int key_right_pressed(void)
{
	return !(readl(S5PV210_GPH2DAT) & (0x1<<1));
}

int key_back_pressed(void)
{
	return !(readl(S5PV210_GPH2DAT) & (0x1<<2));
}

int key_up_pressed(void)
{
	return !(readl(S5PV210_GPH2DAT) & (0x1<<0));
}

int key_menu_pressed(void)
{
	return !(readl(S5PV210_GPH2DAT) & (0x1<<3));
}

void key_led_test()
{
	led_init();
	key_init();
	while(1)
	{
		if(readl(S5PV210_GPH0DAT) & (0x1<<2))	//GPH0_2，LEFT
		{
			led_set(0,0);
		}
		else
		{
			led_set(0,1);
		}
		if(readl(S5PV210_GPH0DAT) & (0x1<<3))	//GPH0_3，DOWN
		{
			led_set(1,0);
		}
		else
		{
			led_set(1,1);
		}
		if(readl(S5PV210_GPH2DAT) & (0x1<<1))	//GPH2_1，RIGHT
		{
			led_set(2,0);
		}
		else
		{
			led_set(2,1);
		}
		if(readl(S5PV210_GPH2DAT) & (0x1<<2))	//GPH2_2，BACK
		{
			led_set(3,0);
		}
		else
		{
			led_set(3,1);
		}
	}
}


void key_led_buzzer_test(void)
{
	led_init();
    key_init();
	speaker_init();
	while(1)
	{
		if(readl(S5PV210_GPH0DAT) & (0x1<<2))	//GPH0_2，LEFT
		{
			led_set(0,0);//led1 off
		}
		else
		{
			led_set(0,1);//led1 on
		}
		if(readl(S5PV210_GPH0DAT) & (0x1<<3))	//GPH0_3，DOWN
		{
			led_set(1,0);//led2 off
		}
		else
		{
			led_set(1,1);//led2 on
		}
		if(readl(S5PV210_GPH2DAT) & (0x1<<1))	//GPH2_1，RIGHT
		{
			led_set(2,0);//led3 off
		}
		else
		{
			led_set(2,1);//led4 on
		}
		if(readl(S5PV210_GPH2DAT) & (0x1<<2))	//GPH2_2，BACK
		{
			led_set(3,0);//led4 off
		}
		else
		{
			led_set(3,1);//led4 on
		}
		if(readl(S5PV210_GPH2DAT) & (0x1<<0))	//GPH2_0，UP
		{
			//led off and buzzer off
			led_set(0,0);
			led_set(1,0);
			led_set(2,0);
			led_set(3,0);
			speaker_set(0);
		}
		else
		{
			//led on and buzzer on
			led_set(0,1);
			led_set(1,1);
			led_set(2,1);
			led_set(3,1);
			speaker_set(1);
		}
		if(readl(S5PV210_GPH2DAT) & (0x1<<3))	//GPH2_3，MENU
		{
		}
		else
		{
			led_set(0,1);
			led_set(1,1);
			led_set(2,1);
			led_set(3,1);
			speaker_set(1);
			mdelay(2000);
			led_set(0,0);
			led_set(1,0);
			led_set(2,0);
			led_set(3,0);
			speaker_set(0);
			mdelay(2000);
		}
	}
}
